package com.framework.boc.admin.config;

import com.framework.boc.admin.security.filters.JwtRequestFilter;
import com.framework.boc.admin.security.service.MyUserDetailsService;
import com.framework.boc.admin.security.sms.handler.SmsCodeAuthenticationExceptionHandler;
import com.framework.boc.admin.security.sms.handler.SmsCodeAuthenticationFailureHandler;
import com.framework.boc.admin.security.sms.SmsCodeAuthenticationFilter;
import com.framework.boc.admin.security.sms.SmsCodeAuthenticationProvider;
import com.framework.boc.admin.security.sms.handler.SmsCodeAuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.AntPathMatcher;

import java.io.PrintWriter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true, prePostEnabled = true, securedEnabled = true)   //使用jsr-250注解控制权限
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    SmsCodeAuthenticationProvider smsCodeAuthenticationProvider;

    @Autowired
    SmsCodeAuthenticationSuccessHandler smsCodeAuthenticationSuccessHandler;

    @Autowired
    SmsCodeAuthenticationFailureHandler smsCodeAuthenticationFailureHandler;

    @Autowired
    SmsCodeAuthenticationExceptionHandler smsCodeAuthenticationExceptionHandler;


    @Bean
    protected SmsCodeAuthenticationFilter authenticationFilter() throws Exception {
        SmsCodeAuthenticationFilter authenticationFilter = new SmsCodeAuthenticationFilter();
        authenticationFilter.setAuthenticationManager(authenticationManager());
        authenticationFilter.setAuthenticationSuccessHandler(smsCodeAuthenticationSuccessHandler);
        authenticationFilter.setAuthenticationFailureHandler(smsCodeAuthenticationFailureHandler);
        return authenticationFilter;
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(smsCodeAuthenticationProvider);
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests().antMatchers("/sms/code", "/contract/verify").permitAll()
                .anyRequest().authenticated()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(jwtRequestFilter(), SmsCodeAuthenticationFilter.class)
                .exceptionHandling().authenticationEntryPoint(smsCodeAuthenticationExceptionHandler);
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/swagger-ui.html")
                .antMatchers("/v2/**")
                .antMatchers("/webjars/**")
                .antMatchers("/swagger-resources/**");
    }

    @Bean
    public AntPathMatcher antPathMatcher() {
        return new AntPathMatcher();
    }

    @Bean
    public JwtRequestFilter jwtRequestFilter() {
        JwtRequestFilter jwtRequestFilter = new JwtRequestFilter();
        jwtRequestFilter.antMatchersPermit("/login", "/sms/code", "/contract/verify");
        return jwtRequestFilter;
    }
}
